package acme.bloodtime.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import modelo.IPocionDAO;
import dao.api.DataObject;

/**
 * 
 * @author Manuel A. Quintero V.
 * 
 */

public class PocionDAO extends ArticuloAbstractDAO implements IPocionDAO {

    public PocionDAO() {
        // Empty
    }

    @Override
    public void createTable() throws SQLException {
        StringBuffer strbuf;

        ArticuloDAO articuloDAO = new ArticuloDAO();

        // ----------------------------------------

        dropTable();

        // ----------------------------------------
        strbuf = new StringBuffer();

        strbuf.append("CREATE TABLE ");
        strbuf.append(getTableName());
        strbuf.append(" (");
        strbuf.append(articuloDAO.getTableName() + PocionDO.ID);
        strbuf.append("  INT REFERENCES ");
        strbuf.append(articuloDAO.getTableName());
        strbuf.append(" (");
        strbuf.append(PocionDO.ID);
        strbuf.append("),");
        strbuf.append(PocionDO.ATRIBUTO_OBJETIVO);
        strbuf.append(" INT,    ");
        strbuf.append(PocionDO.CANT_MEJORA_ATRIBUTO_OBJ);
        strbuf.append(" INT,    ");
        strbuf.append(" PRIMARY KEY   ");
        strbuf.append(" (");
        strbuf.append(articuloDAO.getTableName() + PocionDO.ID);
        strbuf.append("))");

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());

    }

    @Override
    public void insert(DataObject dataObject) throws SQLException {
        checkCache(dataObject, CHECK_INSERT);
        checkClass(dataObject, PocionDO.class, CHECK_INSERT);

        PocionDO pocionDO = (PocionDO) dataObject;

        pocionDO.setId(internalInsertArticulo(dataObject, PocionDO.class));
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("INSERT INTO ");
        strbuf.append(getTableName());
        strbuf.append(" VALUES (");
        strbuf.append(pocionDO.getId());
        strbuf.append(", ");
        strbuf.append(pocionDO.getAtributoObjetivo());
        strbuf.append(", ");
        strbuf.append(pocionDO.getCantMejoraAtributoObj());
        strbuf.append(")");

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());

        dtaSession.add(dataObject);

    }

    @Override
    public void update(DataObject dataObject) throws SQLException {
        checkCache(dataObject, CHECK_UPDATE);
        checkClass(dataObject, PocionDO.class, CHECK_UPDATE);

        ArticuloDAO articuloDAO = new ArticuloDAO();

        PocionDO pocionDO = (PocionDO) dataObject;

        StringBuffer strbuf = new StringBuffer();

        strbuf.append("UPDATE ");
        strbuf.append(getTableName());
        strbuf.append(" SET ");

        strbuf.append(PocionDO.ATRIBUTO_OBJETIVO);
        strbuf.append(" = ");
        strbuf.append(pocionDO.getAtributoObjetivo());

        strbuf.append(", ");

        strbuf.append(PocionDO.CANT_MEJORA_ATRIBUTO_OBJ);
        strbuf.append(" = ");
        strbuf.append(pocionDO.getCantMejoraAtributoObj());

        strbuf.append(" WHERE ");
        strbuf.append(articuloDAO.getTableName() + PocionDO.ID);
        strbuf.append(" = ");
        strbuf.append(pocionDO.getId());

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());

        internalUpdateArticulo(dataObject, PocionDO.class);

    }

    @Override
    public void delete(DataObject dataObject) throws SQLException {
        checkCache(dataObject, CHECK_DELETE);
        checkClass(dataObject, PocionDO.class, CHECK_DELETE);

        ArticuloDAO articuloDAO = new ArticuloDAO();

        StringBuffer strbuf = new StringBuffer();

        strbuf.append("DELETE FROM ");
        strbuf.append(getTableName());

        strbuf.append(" WHERE ");
        strbuf.append(articuloDAO.getTableName() + DataObject.ID);
        strbuf.append(" = ");
        strbuf.append(dataObject.getId());

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());
        internalDelete(dataObject, PocionDO.class);
        dtaSession.del(dataObject);

    }

    @Override
    protected DataObject resultSetToDO(ResultSet rs) throws SQLException {
        PocionDO ret = //
        (PocionDO) dtaSession.getDtaByKey( //
                PocionDO.class, rs.getInt(PocionDO.ID));
        if (ret != null) {
            return ret;
        }
        
        ret = new PocionDO();

        ret.setId/*                     */(rs.getInt(PocionDO.ID));
        ret.setNombre/*                 */(rs.getString(PocionDO.NOMBRE));
        ret.setDescripcion/*            */(rs.getString(PocionDO.DESCRIPCION));
        ret.setImagen/*                 */(rs.getBytes(PocionDO.IMAGEN));
        ret.setPrecioCompra/*           */(rs.getFloat(PocionDO.PRECIO_COMPRA));
        ret.setPrecioVenta/*            */(rs.getFloat(PocionDO.PRECIO_VENTA));
        ret.setAtributoObjetivo/*       */(rs.getInt(PocionDO.ATRIBUTO_OBJETIVO));
        ret.setCantMejoraAtributoObj/*  */(rs.getInt(PocionDO.CANT_MEJORA_ATRIBUTO_OBJ));

        return (PocionDO) dtaSession.add(ret);
    }

}
